package org.summerframework.component.upload.smb;

import org.summerframework.component.upload.AbstractUploadServerService;
import org.summerframework.component.upload.FileUploadServerService;
import org.summerframework.component.upload.UploadResultCode;
import org.summerframework.core.exception.WebException;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
import jcifs.smb.SmbFileOutputStream;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;

/**
 * @author 石超
 * @version v1.0.0
 */
@Slf4j
public class SmbUploadServerServiceImpl extends AbstractUploadServerService implements FileUploadServerService {
    private SmbProperties properties;

    public SmbUploadServerServiceImpl(SmbProperties properties) {
        this.properties = properties;
        this.properties.setPath(FilenameUtils.concat("/", properties.getPath()));
    }

    @Override
    public String uploadFile(InputStream is, String fileName) {
        String path = getPath(fileName);
        String savePth = getPath(properties.getPath(), path);

        try {
            SmbFile smbFile = getSmbFile(FilenameUtils.getFullPath(savePth));
            if (!smbFile.exists()) {
                smbFile.mkdirs();
            }

            smbFile.connect();

            try (OutputStream outputStream = new SmbFileOutputStream(getSmbFile(savePth), false)) {
                IOUtils.copy(is, outputStream);
                outputStream.flush();
            }
        } catch (IOException e) {
            throw new WebException(UploadResultCode.ERROR_UPLOAD_FAILURE, e);
        }
        return path;
    }

    @Override
    public byte[] getFileBytes(String path) {
        try {
            path = getPath(properties.getPath(), path);
            SmbFile smbFile = getSmbFile(path);
            try (InputStream smbFileInputStream = new SmbFileInputStream(smbFile)) {
                return IOUtils.toByteArray(smbFileInputStream);
            }
        } catch (Exception e) {
            return EMPTY_BYTES;
        }
    }

    @Override
    public InputStream getInputStream(String path) throws Exception {
        path = getPath(properties.getPath(), path);
        SmbFile smbFile = getSmbFile(path);
        return new SmbFileInputStream(smbFile);
    }

    @Override
    public void deleteFile(String path) {
        try {
            path = getPath(properties.getPath(), path);
            SmbFile smbFile = getSmbFile(path);
            smbFile.delete();
        } catch (Exception e) {
            log.warn("删除文件是不", e);
        }
    }

    private SmbFile getSmbFile(String path) {
        try {
            if (StringUtils.isBlank(properties.getPassword())) {
                return new SmbFile(String.format("smb://%s@%s%s", properties.getAccount(), properties.getHost(), path));
            } else {
                return new SmbFile(String.format("smb://%s:%s@%s%s", properties.getAccount(), properties.getPassword(), properties.getHost(), path));
            }
        } catch (MalformedURLException e) {
            throw new WebException(UploadResultCode.ERROR_INIT_CLIENT, e);
        }
    }
}
